home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / uhren & terminkalender / kalender / cgcalendar / cgcaldos.c < prev    next >
C/C++ Source or Header  |  1996-04-07  |  4KB  |  166 lines

  1.  
  2. /*
  3. ***                         --- CGCalendar v1.0 ---
  4. **
  5. ***                 Copyright Craig G. Callan (07.30.94)
  6. **
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <time.h>
  12.  
  13.     const       earliest = 1991, offset = 0;
  14.  
  15.     int         day, julian, leapflg, month, wkday, wkdayfirst, year;
  16.     int         monthlen [12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  17.     char        buf [80], yr [4], wkname [10], weekname [8][10], mname [12], monthname [13][12];
  18.     time_t      timeDate;
  19.     struct tm   *localTime;
  20.     char        datestring[80];
  21.  
  22. void main ()
  23. {
  24.     int     i, j;
  25.  
  26.     initialize ();
  27.     leapflg = checkforleapyear (year);
  28.     julian = findjulian (day, month);
  29.     drawcal ();
  30. }
  31.  
  32. /*
  33. ***
  34. */
  35.  
  36. int checkforleapyear (yr)
  37. {
  38.     if ((yr % 4) == 0)
  39.     {                     /* Check for leap year and alter February MONTHLEN */
  40.         monthlen [2] = 29;
  41.         return 1;
  42.     }
  43.     else
  44.     {                     /* Also set LEAPFLG to TRUE or FALSE as needed     */
  45.         monthlen [2] = 28;
  46.         return 0;
  47.     }
  48. }
  49.  
  50. drawcal ()
  51. {
  52.     int     i, j;
  53.  
  54.     puts (" ");
  55.     strcopy (buf, monthname [month]); strcat (buf, " "); strcat (buf, yr);
  56.     if (leapflg == 1) strcat (buf, " *");
  57.     j = 13-((strlen (buf) + 6)/2);
  58.     for (i = 1; i < j; i++)
  59.         printf (" ");
  60.     printf ("--- %s ---\n", buf);
  61.     j = wkdayfirst;
  62.     for (i = 1; i < j; i++)
  63.         printf ("    ");
  64.     for (i = 1; i < (monthlen [month - 1] + 1); i++)
  65.     {
  66.         if (i < 10)
  67.         {
  68.             if (i != day)
  69.                 printf ("  %d ", i);
  70.             else
  71.                 printf (" [%d]", i);
  72.         }
  73.         else
  74.         {
  75.             if (i != day)
  76.                 printf (" %d ", i);
  77.             else
  78.                 printf ("[%d]", i);
  79.         }
  80.         j++;
  81.         if (j > 7)
  82.         {
  83.             j = 1;
  84.             printf ("\n");
  85.         }
  86.     }
  87.     printf ("\n\n");
  88. }
  89.  
  90. int findjulian (dy, mnth) /* Returns the julian date given the day and month */
  91. {
  92.     int     jul, i;
  93.  
  94.     jul = dy;
  95.     for (i = 1; i < mnth - 1; i++)
  96.         jul = jul + monthlen [i];
  97.     return jul;
  98. }
  99.  
  100. initialize ()
  101. {
  102.     int         i, j;
  103.     char        buf [4];
  104.  
  105.     strcopy (weekname [1], "Sunday"); strcopy (weekname [2], "Monday");
  106.     strcopy (weekname [3], "Tuesday"); strcopy (weekname [4], "Wednesday");
  107.     strcopy (weekname [5], "Thursday"); strcopy (weekname [6], "Friday");
  108.     strcopy (weekname [7], "Saturday");
  109.     strcopy (monthname [1], "January"); strcopy (monthname [2], "February");
  110.     strcopy (monthname [3], "March"); strcopy (monthname [4], "April");
  111.     strcopy (monthname [5], "May"); strcopy (monthname [6], "June");
  112.     strcopy (monthname [7], "July"); strcopy (monthname [8], "August");
  113.     strcopy (monthname [9], "September"); strcopy (monthname [10], "October");
  114.     strcopy (monthname [11], "November"); strcopy (monthname [12], "December");
  115.     timeDate = time (NULL);
  116.     localTime = localtime (&timeDate);
  117.     strftime (datestring, (size_t)80, "\"%A, %d %B %Y\"\n", localTime );
  118.     i = 0;
  119.     while (datestring [i] != 'y' && datestring [i] != 0)
  120.     {
  121.         wkname [i] = datestring [i + 1];
  122.         i++;
  123.         wkname [i] = 0;
  124.     }
  125.     wkday = 0;
  126.     for (j = 1; j < 8; j++)
  127.         if (strcmp (weekname [j], wkname) == 0) wkday = j;
  128.     i = i + 3; j = 0;
  129.     while (datestring [i] != ' ' && datestring [i] != 0)
  130.     {
  131.         buf [j] = datestring [i];
  132.         i++; j++;
  133.         buf [j] = 0;
  134.     }
  135.     day = atoi (buf);
  136.     i++; j = 0;
  137.     while (datestring [i] != ' ' && datestring [i] != 0)
  138.     {
  139.         mname [j] = datestring [i];
  140.         i++; j++;
  141.         mname [j] = 0;
  142.     }
  143.     month = 0;
  144.     for (j = 1; j < 13; j++)
  145.         if (strcmp (monthname [j], mname) == 0) month = j;
  146.     i++; j = 0;
  147.     while (j < 4)
  148.     {
  149.         yr [j] = datestring [i];
  150.         i++; j++;
  151.         yr [j] = 0;
  152.     }
  153.     year = atoi (yr);
  154.     i = day; wkdayfirst = wkday;
  155.     while (i > 1)
  156.     {
  157.         i--; wkdayfirst--;
  158.         if (wkdayfirst < 1) wkdayfirst = 7;
  159.     }
  160. }
  161.  
  162. strcopy (t, s)  register    char    *t, *s;
  163. {
  164.     while (*t++ = *s++);
  165. }
  166.